package jee.boot.module.authority.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.module.authority.dao.RowSetDao;
import jee.boot.module.authority.dao.RowSetItemDao;
import jee.boot.entity.auth.RowSet;
import jee.boot.entity.auth.RowSetItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

/**
 * 记录集
 */
@Service
public class RowSetService extends CrudService<RowSetDao, RowSet> {
    @Autowired
    private RowSetItemDao rowSetItemDao;

    @Override
    public RowSet get(RowSet entity) {
        entity = super.get(entity);
        getMany(entity);
        return entity;
    }

    public void getMany(RowSet entity) {
        entity.setItemList(super.getOneToMany(rowSetItemDao, entity, new RowSetItem(), "b.st_header_id"));
    }

    @Override
    public void save(RowSet entity) {
        super.save(entity);
        super.saveOneToMany(rowSetItemDao, entity, "header", entity.getItemList());
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void update(RowSet entity) {
        super.update(entity);
        super.updateOneToMany(rowSetItemDao, entity, new RowSetItem(), "header", entity.getItemList());
    }

    @Caching(evict = {
            @CacheEvict(value = AuthorityHandler.USER_AUTHORITY_KEY, allEntries = true)
    })
    @Override
    public void delete(RowSet entity) {
        super.delete(entity);
        super.deleteOneToMany(rowSetItemDao, entity, new RowSetItem(), "header");
    }
}
